/**
 ******************************************************************************
 * @file app_photo.c
 * @author GX (2524913364@qq.com)
 * @brief   相册应用有两个子界面
 *          1.总览界面：
 *            （1）会有四张被压缩的照片显示在画面中
 *             (2) 左滑或者右滑屏幕可以每次切换两张图图片
 *             (3) 上滑退出应用到桌面
 *             (4) 点击任意张图片进入卷轴子界面
 *          2.卷轴界面：
 *             (1) 横向卷轴，一张卷轴包含三张照片。
 *             (2) 显示在画面中只会有一张图的宽度,拖动卷轴可以移动卷轴可视位置
 *             (3) 拖动到卷轴边缘后会刷新卷轴中的图片，实现卷轴可以查看所有照片。
 * @version 1.0.0 
 * @date 2022-07-11
 * 
 * @copyright Copyright (c) 2022
 * 
 ******************************************************************************
 *----------------------------------------------------------------------------*
 *  Remark         : Description                                              *
 *----------------------------------------------------------------------------*
 *  Change History :                                                          *
 *  <Date>     | <Version> | <Author>       | <Description>                   *
 *----------------------------------------------------------------------------*
 *  2022-07-11 | 1.0.0     | GX             | Create file                     *
 *----------------------------------------------------------------------------*
 *                                                                            *
 ******************************************************************************
 */

/*----------------------------- start of file -------------------------------*/
#include "app_photo.h"

#include "lcd.h"
#include "touch_screen.h"

/* 总览界面的函数声明----------------------------------------------------- */
static PHOTO_INTERFACE_TRUN overviewPhoto(int *fb_base, int fd,
                                          struct fb_var_screeninfo *vinfo);
static void putPhotoToFram(int *fb_base, int fd,
                            struct fb_var_screeninfo *vinfo);
static OVERVIEW_KEY unlock_GetKey(int x, int y);       

/* 卷轴界面的函数声明----------------------------------------------------- */
static PHOTO_INTERFACE_TRUN scrollPhoto(int *fb_base, int fd,
                                        struct fb_var_screeninfo *vinfo);
static void draw_srcollbuf(int srcoll_block);                 
static void view_srcoll(int *fb_base, int view_l, int view_r);

static int searchDir( void );


/* 全局变量定义---------------------------------------------------------- */
/* 整个 app 都要用到的全局变量 */ 
static int photo_num = 0;               /* 路径中 .bmp 图片的总数 */ 
static DCLinkList *photo_list = NULL;   /* 图片列表 */
static DCLinkList *photo_search = NULL; /* 查找图片的结果 */

/* 总览界面专用的变量 */
static int fram_data[4] = {0}; /* 记录四个相框里分别放了第几张照片，要其它函数来初始化 */

/* 卷轴界面需要用的变量 */
static int scroll_data[SRCOLL_SIZE];  /* 记录卷轴中各部分存的图片 */
static int scroll_buf[LCD_HEIGHT][LCD_WIDE*SRCOLL_SIZE] = {0};  /* 卷轴内容 */


/*******************************************************************************
 * 
 *                                 APP应用，总函数
 * 
*******************************************************************************/

/**
 * @brief app总函数，是管理app切换到各个子界面的
 * 
 * @return true 
 * @return false 
 */
bool app_photo(void)
{
  printf("in to photo\n");
  /*---------------------------------开启显示屏幕-------------------------------*/
  int fd_lcd;                             /* 打开文件获取文件描述 */
  int *fb_base = NULL;                    /* 映射后显存的地址 */
  struct fb_var_screeninfo vinfo;         /* 显卡设备的可变属性结构体 */
	/* 打开文件 */
	fd_lcd = open(LCD_PATH, O_RDWR);
	if(fd_lcd == -1)
	{
		perror("open lcd");
    return false;
	}
  else
	{
		/* 显存映射 */
		fb_base = mmap(NULL, LCD_BUFF_SIZE*LCD_BLOCK,  PROT_READ|PROT_WRITE,
					MAP_SHARED, fd_lcd, 0);
		if(fb_base == MAP_FAILED) 
		{
			perror("申请显存失败");
      close(fd_lcd);
      return false;
		} 

    if(ioctl(fd_lcd, FBIOGET_VSCREENINFO, &vinfo) != 0)/* 获取可变属性 */
    {
        perror("获取LCD设备可变属性信息失败");
        return false;
    }
  }

  /*--------------------------------正式开始相册相关-----------------------------*/

  PHOTO_INTERFACE_TRUN photo_itf = PHOTO_OVERVIEW;

  /* 初始化图片链表头节点 */
  datatype tmpdata;
  tmpdata.num = 0;
  strcpy(tmpdata.bmp_name, "nodata");
  photo_list = createNode(tmpdata);

  /* 查找文件夹下的所有 .bmp 图片，将路径存入链表 */
  searchDir();

  /* 更新状态机 */
  Renew_tcState();

  while (1) 
  {
    switch(photo_itf) /* 切换到各个子界面 */
    {
    case PHOTO_OVERVIEW:
      photo_itf = overviewPhoto(fb_base, fd_lcd, &vinfo);
      break;

    case PHOTO_SCROLL:
      photo_itf = scrollPhoto(fb_base, fd_lcd, &vinfo);
      break;

    case PHOTO_BACK_TO_DESTTOP:

      break;
    }

    if( photo_itf == PHOTO_BACK_TO_DESTTOP )
    {
      break;
    }
  }
  /* while(1) end--------------------------------------------------------- */

  destoryLink(photo_list);      /* 销毁链表 */
  view_block = 0;
  printf("destory finish\n");
  munmap(fb_base, LCD_BUFF_SIZE*LCD_BLOCK);
  printf("munmap finish\n");
  close(fd_lcd);
  
  printf("out photo\n");

  return true;
}
 

/*******************************************************************************
 * 
 *                               APP应用子界面————总览界面
 * 
*******************************************************************************/

/**
 * @brief 相册的总览界面
 * 
 * @param fb_base 显存映射的基地址
 * @param fd      显示屏的文件描述符
 * @param vinfo   显示屏可变属性的指针
 * @return PHOTO_INTERFACE_TRUN 
 */
static PHOTO_INTERFACE_TRUN overviewPhoto(int *fb_base, int fd,
                                          struct fb_var_screeninfo *vinfo)
{
  printf("into overviewPhoto\n");
  PHOTO_INTERFACE_TRUN itface = PHOTO_OVERVIEW;
  OVERVIEW_KEY key = OVERVIEW_NONE;     /* 键值 */

  
  for(int i=0; i<4; i++)  /* for() 绘制相框 */
  {
    // LCD_FULL(LCD_DISPLAY(fb_base, ++view_block), LCD_BLACK);
    lcd_rectangle(LCD_DISPLAY(fb_base, ++view_block), 
                  0, 0,
                  LCD_WIDE, LCD_HEIGHT, 
                  PHOTO_BACK_COLOR);
    // usleep(1000);
    /* 第一个相框 */
    lcd_rectangle(LCD_DISPLAY(fb_base, view_block), 
                  PHOTO_FRAM_1_X_START, PHOTO_FRAM_1_Y_START,
                  PHOTO_FRAM_WIDTH, PHOTO_FRAM_HEIGHT, 
                  PHOTO_FRAME_COLOR);
    // usleep(1000);              
    /* 第二个相框 */
    lcd_rectangle(LCD_DISPLAY(fb_base, view_block), 
                  PHOTO_FRAM_2_X_START, PHOTO_FRAM_2_Y_START,
                  PHOTO_FRAM_WIDTH, PHOTO_FRAM_HEIGHT, 
                  PHOTO_FRAME_COLOR);
    // usleep(1000);
    /* 第三个相框 */
    lcd_rectangle(LCD_DISPLAY(fb_base, view_block), 
                  PHOTO_FRAM_3_X_START, PHOTO_FRAM_3_Y_START,
                  PHOTO_FRAM_WIDTH, PHOTO_FRAM_HEIGHT, 
                  PHOTO_FRAME_COLOR);
    // usleep(1000);
    /* 第四个相框 */
    lcd_rectangle(LCD_DISPLAY(fb_base, view_block), 
                  PHOTO_FRAM_4_X_START, PHOTO_FRAM_4_Y_START,
                  PHOTO_FRAM_WIDTH, PHOTO_FRAM_HEIGHT, 
                  PHOTO_FRAME_COLOR);
    // usleep(1000);
  }
  /* end for() 绘制相框 */
  putPhotoToFram(fb_base, fd, vinfo);
  LCD_SET_DISPLAY( fd, vinfo, view_block);


  TOUCH *tc = malloc(sizeof(TOUCH));
  while(1)
  {
    get_tcState(tc);
    if( tc->response )
    {
      // printf("respongse\n");
      continue;
    }
    switch (tc->state)
    {
    case STATE_SINGLE_CLICK:  /* 单击,确定用户要查看哪张图片 */
      /* 响应状态 */
      printf("photo STATE_SINGLE_CLICK\n");
      response_over();

      key = unlock_GetKey(tc->x_last, tc->y_last);
      switch (key)
      {
      case OVERVIEW_FRAM_1:
        if((fram_data[0] > 0) && (fram_data[0] <= photo_num))
        {
          scroll_data[0] = fram_data[0]-1;
          scroll_data[1] = fram_data[0];
          scroll_data[2] = fram_data[0]+1;
        }
        itface = PHOTO_SCROLL;
        break;
      
      case OVERVIEW_FRAM_2:
        if((fram_data[1] > 0) && (fram_data[1] <= photo_num))
        {
          scroll_data[0] = fram_data[1]-1;
          scroll_data[1] = fram_data[1];
          scroll_data[2] = fram_data[1]+1;
        }
        itface = PHOTO_SCROLL;
        break;

      case OVERVIEW_FRAM_3:
        if((fram_data[2] > 0) && (fram_data[2] <= photo_num))
        {
          scroll_data[0] = fram_data[2]-1;
          scroll_data[1] = fram_data[2];
          scroll_data[2] = fram_data[2]+1;
        }
        itface = PHOTO_SCROLL;
        break;

      case OVERVIEW_FRAM_4:
        if((fram_data[3] > 0) && (fram_data[3] <= photo_num))
        {
          scroll_data[0] = fram_data[3]-1;
          scroll_data[1] = fram_data[3];
          scroll_data[2] = fram_data[3]+1;
        }
        itface = PHOTO_SCROLL;
        break;

      default:
        break;
      }

      break;
      
    case STATE_SLIDING:				/* 滑屏 */
      /* 响应状态 */
      printf("photo STATE_SLIDING\n");
      response_over();
      switch (tc->motion)    
      {
      case TC_LEFT:   /* 左滑 */
        printf("left");
        if(fram_data[3] < photo_num)
        {
          fram_data[3] += 2;
          fram_data[2] = fram_data[3]-1;
          fram_data[1] = fram_data[2]-1;
          fram_data[0] = fram_data[1]-1;
          putPhotoToFram(fb_base, fd, vinfo);
        }
        break;
      
      case TC_RIGHT:  /* 右滑 */
        printf("right");
        if(fram_data[0] > 0)
        {
          fram_data[0] -= 2;
          fram_data[1] = fram_data[0]+1;
          fram_data[2] = fram_data[1]+1;
          fram_data[3] = fram_data[2]+1;
          putPhotoToFram(fb_base, fd, vinfo);
        }
        break;

      case TC_UP:     /* 上滑 */
        printf("up");
        itface = PHOTO_BACK_TO_DESTTOP;
        break;

      case TC_DOWN:   /* 下滑 */
        printf("down");
        break;

      default:
        break;
      }   /* 子 switch */
      break;
    
    default:
      response_over();
      break;
    }


    printf("scroll_data %d %d %d\n", scroll_data[0], scroll_data[1], scroll_data[2]);
    if( itface != PHOTO_OVERVIEW )
    {
      break;
    }
  }
  /* while(1) end--------------------------------------------------------- */

  printf("out overviewPhoto\n");
  return itface;
}


/**
 * @brief 将照片放到相框，只给 overviewPhoto() 调用
 * 
 */
static void putPhotoToFram(int *fb_base, int fd,
                            struct fb_var_screeninfo *vinfo)
{
  /* 将四个照片起始坐标存进数组，为了方便写成for循环，相对于相框起始坐标要有点偏移 */
  int fram_pos[4][2] = { PHOTO_FRAM_1_X_START+PHOTO_DELTA, 
                         PHOTO_FRAM_1_Y_START+PHOTO_DELTA,
                         PHOTO_FRAM_2_X_START+PHOTO_DELTA, 
                         PHOTO_FRAM_2_Y_START+PHOTO_DELTA,
                         PHOTO_FRAM_3_X_START+PHOTO_DELTA, 
                         PHOTO_FRAM_3_Y_START+PHOTO_DELTA,
                         PHOTO_FRAM_4_X_START+PHOTO_DELTA, 
                         PHOTO_FRAM_4_Y_START+PHOTO_DELTA, };
                   
  char path[150];
  datatype tmpdata;
  DCLinkList *tmpNode;

/* 往四个相框放图片----------------- */  
  for(int i = 0; i < 4; i++)
  {
    /* 通过链表获取 bmp 文件名 */
    tmpdata.num = fram_data[i];
    printf("search %d\n", fram_data[i]);
    tmpNode = searchNode(photo_list, tmpdata, cmpNode);
    if(tmpNode != NULL)
    {
      /* 拼接路径 */
      printf("search res %s\n", tmpNode->data->bmp_name);
      sprintf(path, "%s%s", PHOTO_PATH, tmpNode->data->bmp_name);
      printf("search path:%s\n", path);

      /* 调用函数将相片放到相框 */
      lcd_show_zipbmp(LCD_DISPLAY(fb_base, view_block), 
                      path, fram_pos[i][0], fram_pos[i][1], 4);
    }
    else  
    {
      /* 如果某个相框是没有照片了就刷新显示相框覆盖掉 */
      lcd_rectangle(LCD_DISPLAY(fb_base, view_block),
                      fram_pos[i][0], fram_pos[i][1],
                      PHOTO_FRAM_WIDTH-PHOTO_DELTA, 
                      PHOTO_FRAM_HEIGHT-PHOTO_DELTA, 
                      PHOTO_FRAME_COLOR);
    }
  }

  return;
}

/**
 * @brief 获取键值 只给 overviewPhoto() 调用
 * 
 * @param x 触摸屏x坐标
 * @param y 触摸屏y坐标
 * @return OVERVIEW_KEY 
 */
static OVERVIEW_KEY unlock_GetKey(int x, int y)
{
  OVERVIEW_KEY key = OVERVIEW_NONE;

  /* 相框1 */
  if(( x >= PHOTO_FRAM_1_X_START ) && ( x <= PHOTO_FRAM_1_X_END ) &&
      ( y >= PHOTO_FRAM_1_Y_START ) && ( y <= PHOTO_FRAM_1_Y_END ))
  {
    key = OVERVIEW_FRAM_1;
    printf("fram 1\n");
  }
    
  
  /* 相框2 */
  if(( x >= PHOTO_FRAM_2_X_START ) && ( x <= PHOTO_FRAM_2_X_END ) &&
      ( y >= PHOTO_FRAM_2_Y_START ) && ( y <= PHOTO_FRAM_2_Y_END ))
  {
    key = OVERVIEW_FRAM_2;
    printf("fram 2\n");
  }
    

  /* 相框3 */
  if(( x >= PHOTO_FRAM_3_X_START ) && ( x <= PHOTO_FRAM_3_X_END ) &&
      ( y >= PHOTO_FRAM_3_Y_START ) && ( y <= PHOTO_FRAM_3_Y_END ))
  {
    key = OVERVIEW_FRAM_3;
    printf("fram 3\n");
  }

  /* 相框4 */
  if(( x >= PHOTO_FRAM_4_X_START ) && ( x <= PHOTO_FRAM_4_X_END ) &&
      ( y >= PHOTO_FRAM_4_Y_START ) && ( y <= PHOTO_FRAM_4_Y_END ))
  {
    key = OVERVIEW_FRAM_4;
    printf("fram 4\n");
  }

  return key;
}

/*******************************************************************************
 * 
 *                             APP应用子界面————卷轴界面（大图）
 * 
*******************************************************************************/

/**
 * @brief 卷轴界面
 * 
 * @param fb_base 显存映射的基地址
 * @param fd      显示屏的文件描述符
 * @param vinfo   显示屏可变属性的指针
 * @return PHOTO_INTERFACE_TRUN 
 */
static PHOTO_INTERFACE_TRUN scrollPhoto(int *fb_base, int fd,
                                          struct fb_var_screeninfo *vinfo)
{
  printf("into appriciatePhoto\n");
  bool back = false;
  int srcollView_Left = LCD_WIDE, srcollView_Right = LCD_WIDE*2;  /* 卷轴视野的左右边界 */
  
  draw_srcollbuf(0);
  draw_srcollbuf(1);
  draw_srcollbuf(2);

  view_srcoll(LCD_DISPLAY(fb_base, ++view_block), srcollView_Left, srcollView_Right);
  LCD_SET_DISPLAY(fd, vinfo, view_block);

  /* 更新状态机 */
  Renew_tcState();
  TOUCH *tc = malloc(sizeof(TOUCH));
  bool flowing = false; /* 判断是否开始在跟随 */
  int x_flow = 0;       /* 记录拖拽时每一刻的前一个坐标， */
  int delta_x = 0;      /* 拖动时 x 的偏移量 */

  int use_x = -1;            /* 调试经验 */
  while(1)
  {
    get_tcState(tc);
    if( tc->response )
    {
      // printf("respongse\n");
      continue;
    }

    switch (tc->state)
    {
    case STATE_PRESSED:  /* 按住 */
      response_over();
      // x_flow = tc->x_last;
      // flowing = false;
      // printf("     x           %d, %d\n", tc->x, tc->x_last);
      break;
    
    case STATE_DTAGING:  /* 拖动 */
      // response_over();
      if( !flowing )
      {
        flowing = true;
        break;
      }
      /* 把 delta_x 限制在 -60 ～ 60 */
      delta_x = tc->x_last - x_flow;
      delta_x = (delta_x > 60) ? 60 : ( (delta_x < -60) ? -60 :delta_x );

      srcollView_Left -= (int)(delta_x*1.5);
      if(srcollView_Left < SRCOLL_VIEW_L_BOUNDARY)
      {
        if( scroll_data[0] <= 1 ) /* 左边已经没有新的图片了 */
        {
          srcollView_Left = SRCOLL_VIEW_L_BOUNDARY;
        }
        else  /* 左边还有图片就刷新卷轴 */
        {
          scroll_data[0]--;  
          scroll_data[1] = scroll_data[0] + 1;  
          scroll_data[2] = scroll_data[1] + 1;
          draw_srcollbuf(0); draw_srcollbuf(1); draw_srcollbuf(2);
          srcollView_Left = LCD_WIDE;
        }
      }
      if(srcollView_Left > SRCOLL_VIEW_R_BOUNDARY)
      {
        if( scroll_data[2] >= photo_num ) /* 右边已经没有新的图片了 */
        { 
          srcollView_Left = SRCOLL_VIEW_R_BOUNDARY;
        }
        else  /* 左边还有图片就刷新卷轴 */
        {
          scroll_data[2]++;  
          scroll_data[1] = scroll_data[2] - 1;
          scroll_data[0] = scroll_data[1] - 1; 
          draw_srcollbuf(0); draw_srcollbuf(1); draw_srcollbuf(2);
          srcollView_Left = LCD_WIDE;
        }
      }
      srcollView_Right = srcollView_Left + LCD_WIDE;    
      view_srcoll(LCD_DISPLAY(fb_base, ++view_block), srcollView_Left, srcollView_Right);
      LCD_SET_DISPLAY(fd, vinfo, view_block);    

      x_flow = tc->x_last;
      printf("dtaging: x_flow:%d  delta:%d\n", x_flow, delta_x);
      // printf("l:%d   r:%d\n", srcollView_Left, srcollView_Right);

      break;

    case STATE_SLIDING:  /* 向上划屏退出 */
      response_over();
      if( tc->motion == TC_UP )     /* 上滑 */
      {
        printf("up");
        back = true;
      }
      break;
    
    default:
      response_over();
      flowing = false;
      break;

    }

    if( back )
    {
      break;
    }
  }


  printf("out appriciatePhoto\n");
  return PHOTO_OVERVIEW;
}

/**
 * @brief 将一张bmp图像画到卷轴上，目前该功能只支持bmp图像像素大小与屏幕严格一致
 * 
 * @param srcoll_block 要画到卷轴的哪一块
 *        @arg  0 ~ 2
 */
static void draw_srcollbuf(int srcoll_block)
{
  bool draw_pic = false;  /* 画卷轴成功的标志，如果最后是失败的话则会铺满单色 */
  char path[150];
  int  fd_bmp;
  datatype tmpdata;
  DCLinkList *tmpNode;

  /* 通过链表获取 bmp 文件名 */
  tmpdata.num = scroll_data[srcoll_block];
  // printf("search %d\n", scroll_data[0]);
  tmpNode = searchNode(photo_list, tmpdata, cmpNode);
  if(tmpNode != NULL)
  {
    /* 拼接路径 */
    sprintf(path, "%s%s", PHOTO_PATH, tmpNode->data->bmp_name);
    printf("draw_srcoll %s\n", path);
    fd_bmp = open(path, O_RDWR);
    if( fd_bmp != -1 )
    {
      /* 跳过bmp的头54个字节 */
      lseek(fd_bmp, 54, SEEK_SET);

      /* 读取bmp图片的RGB数据 */
      char bmp_buf[LCD_WIDE*LCD_HEIGHT*3];
      int ret = read(fd_bmp,bmp_buf,sizeof(bmp_buf));

      /* 图片数据的转换 */
      int lcd_buf[LCD_HEIGHT*LCD_WIDE];
      for(int n=0; n<LCD_HEIGHT*LCD_WIDE; n++)
        *(lcd_buf+n) = (bmp_buf[3*n+2]<<16) + (bmp_buf[3*n+1]<<8) + bmp_buf[3*n];
    
      /* 关闭bmp图片 */
	    close(fd_bmp);

      for(int j = 0; j < LCD_HEIGHT; j++)
      {
        for(int i = LCD_WIDE*srcoll_block, row = 0;
             i < LCD_WIDE*(srcoll_block+1); i++, row++)
        {
          scroll_buf[LCD_HEIGHT-1-j][i] = lcd_buf[j*LCD_WIDE+row];
        }
      }
      draw_pic = true;
      printf("\n\n\nupdata ok %d  %d\n\n\n", srcoll_block, scroll_data[srcoll_block]);
    }
    // fd_bmp = open(path, O_RDWR);
    // char bmp_buf[infoHeader.width*infoHeader.height*3];
	  // int ret = read(fd_bmp,bmp_buf,sizeof(bmp_buf));
    // /* 将图片数据画到srcol */
    // for(int j = 0; j < LCD_HEIGHT; j++)
    // {
    //   for(int i = 0; i < LCD_WIDE; i++)
    //   {
    //     scroll_buf[j][i] = LCD_RED;
    //   }
    // }
  }
  
  /* 没有将图片成功画到卷轴，就铺满一个颜色 */
  if( !draw_pic )
  {
    printf("\n\n\nupdata error %d  %d\n\n\n", srcoll_block, scroll_data[srcoll_block]);
    for(int j = 0; j < LCD_HEIGHT; j++)
    {
      for(int i = LCD_WIDE*srcoll_block; i < LCD_WIDE*(srcoll_block+1); i++)
      {
        scroll_buf[j][i] = LCD_RED;
      }
    }
  }
  return;
}


/**
 * @brief 显示卷轴的内容
 * 
 * @param fb_base 显存映射的基地址
 * @param view_l  卷轴显示的左边界
 * @param view_r  卷轴显示的右边界
 */
static void view_srcoll(int *fb_base, int view_l, int view_r)
{
  view_l = ( view_l < SRCOLL_VIEW_L_BOUNDARY ) ? SRCOLL_VIEW_L_BOUNDARY : view_l;
  for(int j = 0; j < LCD_HEIGHT; j++)
  {
    for(int i = 0; i < LCD_WIDE; i++)
    {
      *(fb_base + (j*LCD_WIDE) + i) = scroll_buf[j][i+view_l];
    }
  }
}                        

/**
 * @brief 搜索目录中的所有 .bmp 图像
 * 
 * @return int 
 */
static int searchDir( void )
{
  char workPath[256];
  char cmp[5] = ".bmp";
  char last_pos = 0;
  sprintf(workPath, "%s%s", getcwd(workPath, sizeof(workPath)), "/data/photo");
  printf("%s\n", workPath);

  datatype newdata;
  DCLinkList *newNode;
  photo_num = 0;


  /* 目录文件实际上就是个文件，可以用 vim 打开一个目录文件看看里面有什么 */
  /* 1.打开目录文件，获取目录指针 */
  DIR *dirFp = opendir(workPath);
  if(dirFp == NULL)
  {
    perror("opendir error");
    return -1;
  }

  /* 2.不断地读取目录下的一个个文件的信息 */
  //注意：readdir() 每次调用只能获取目录下 一个目录项的内容
  while(1)
  {
    /* 该结构体指针存储了读取到的这一个文件的信息 */
    struct dirent *info = readdir(dirFp);
    if(info == NULL)
    {
      printf("read dir end\n");
      break;
    }
    if(info->d_type == DT_REG)
    {
      int i = 0; last_pos = 0;
      while(info->d_name[i] != '\0')
      {
        if(info->d_name[i] == '.')
        {
          last_pos = i;
        }
        i++;
      }
      if(info->d_name[last_pos] == '.')
      {
        // printf("will cmp %s and %s\n", &info->d_name[i], cmp);
        if( strcmp(&info->d_name[last_pos], cmp) == 0)
        {
          photo_num++;
          newdata.num = photo_num;
          strcpy(newdata.bmp_name, info->d_name);
          // printf("name:%s\n", newdata.bmp_name);
          DCLinkList *node = createNode(newdata); 
          insertTial(photo_list, node); 

          // printf("name:%s\ttype:%c\treclen:%d\toff:%ld\tino:%ld\n", info->d_name, info->d_type, info->d_reclen, info->d_off, info->d_ino);
        }
      }     
    }
  }

  /* 如果相框数据没有值就初始化 */
  if(fram_data[0] == 0)
  {
    for(int i = 0; i < photo_num; i++)
    {
      if(i == 4)
      {
        break;
      }
      fram_data[i] = i+1;
    }
  }

  ergodicLink(photo_list, show, 1); 
  /* 3.关闭目录文件 */
  closedir(dirFp);
}

/*******************************************************************************
 * 
 *                            下面的函数是给链表函数提供的接口
 * 
*******************************************************************************/
/**
 * @brief 链表节点的对比函数
 * 
 * @param list_node 链表中的某个节点
 * @param data      需要跟链表节点对比的数据
 * @return true 
 * @return false 
 */
bool cmpNode(datatype list_node, datatype data)
{
  if( list_node.num == data.num )
  {
    return true;
  }
  else
  {
    return false;
  }
}

/**
 * @brief 链表节点数据显示函数
 * 
 * @param a 要显示的节点数据
 */
void show(datatype a) 
{ 
    printf(" %d %s", a.num, a.bmp_name); 
}

/*------------------------------ end of file --------------------------------*/

